package com.neusoft.ecommerce.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.neusoft.ecommerce.dbhelper.LayuiPager;
import com.neusoft.ecommerce.entity.FunctionType;
import com.neusoft.ecommerce.entity.Goods;
import com.neusoft.ecommerce.service.FunctionTypeService;
import com.neusoft.ecommerce.service.GoodsService;
/**
 * 
 * @author Administrator
 *
 */
@RestController
public class FunctionTypeController {

	private final FunctionTypeService functionTypeService;
	private final GoodsService goodsService;
	
	public FunctionTypeController(FunctionTypeService functionTypeService,GoodsService goodsService) {
		this.functionTypeService = functionTypeService;
		this.goodsService = goodsService;
		
	}
	@GetMapping("/functionType_page")
    public ModelAndView afterSaleServicePage() {
         return new ModelAndView("functionType/functionType-list");
    }
	
    @RequestMapping("/functionType/list")
    @ResponseBody
    public LayuiPager<FunctionType> list(@RequestParam(value = "page", defaultValue = "1") String page,
                                    @RequestParam(value = "limit", defaultValue = "10") String limit) {
        System.out.println(page + "" + limit);
        LayuiPager<FunctionType> pager = new LayuiPager<>(Integer.parseInt(page), Integer.parseInt(limit));
        int count = functionTypeService.findTotal(pager);
        List<FunctionType> list = functionTypeService.findList(pager);
        for (FunctionType functionType : list) {
            System.out.println(functionType);
        }
        pager.setData(list);
        pager.setCount(count);
        pager.setCode(0);
        pager.setMsg("");
        return pager;
    }
    @GetMapping("/addfunctionType")
    public ModelAndView addfunctionType() {
        return new ModelAndView("functionType/functionType-add");
    }
    @RequestMapping("/functionType/goodsList")
    public List<Goods> findIndentList() {
	    return goodsService.findAll();
    }
    
    
    @PostMapping("/functionType/add")
    public Map<String, Object> add(FunctionType functionType) {
        System.out.println(functionTypeService);
        Map<String, Object> map = new HashMap<>(16);
        if (functionTypeService.add(functionType)) {
            map.put("msg","ok");
            map.put("code",0);
        } else {
            map.put("msg","失败");
            map.put("code",1);
        }
        return map;
    }
    @GetMapping("/functionType/edit_page")
    public ModelAndView editPage(FunctionType functionType, Model model) {
        System.out.println(functionType);
        LayuiPager<FunctionType> pager = new LayuiPager<>(1,1);
        pager.setCondition(functionType);
        FunctionType message = functionTypeService.findList(pager).get(0);
        System.out.println(message);
        ModelAndView view = new ModelAndView("functionType/functionType-edit");
        model.addAttribute("functionType", message);
        return view;
    }
    @PostMapping("/functionType/edit")
    public Map<String, Object> edit(FunctionType functionType) {
        System.out.println(functionType);
        Map<String, Object> map = new HashMap<>(16);
        if (functionTypeService.edit(functionType)) {
            map.put("msg","ok");
            map.put("code",0);
        } else {
            map.put("msg","失败");
            map.put("code",1);
        }
        return map;
    }
	@PostMapping("/functionType/delete")
    public Map<String, Object> delete(FunctionType functionType) {
        System.out.println(functionType);
        Map<String, Object> map = new HashMap<>(16);
        if (functionTypeService.delete(functionType)) {
            map.put("msg","ok");
            map.put("code",0);
        } else {
            map.put("msg","失败");
            map.put("code",1);
        }
        return map;
    }
	
}
